// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once


#include <string>
#include <turbo/utility/status.h>
#include <pollux/expression/simple_function_registry.h>

namespace kumo::pollux {
    /// Dynamically opens and registers functions defined in a shared library.
    ///
    /// The library being linked needs to provide a function with the following
    /// signature:
    ///
    /// void registry();
    ///
    /// The registration function needs to be defined in the global namespace,
    /// and be enclosed by a extern "C" directive to prevent the compiler from
    /// mangling the symbol name.

    /// The function uses dlopen to load the shared library.
    /// It then searches for the "void registry()" C function which typically
    /// contains all the registration code for the user-defined Pollux components
    /// such as functions defined in library. After locating the function it
    /// executes the registration bringing the user-defined Pollux components
    /// such as function in the scope of the Pollux runtime.
    ///
    /// Loading a library twice can cause a components to be registered twice.
    /// This can fail for certain Pollux components.
    turbo::Status load_dynamic_library(const std::string &fileName);
} // namespace kumo::pollux
